<?php
// require_once __DIR__ . '/baiyingSDK/open-sdk-php/lib/BYTokenClient.php';
// require_once __DIR__ . '/baiyingSDK/open-sdk-php/lib/BYHttpClient.php';
// $token = 'f33160b4db6d39a3ae2091ce99796372';//请填入后获取的access_token
// $client = new BYTokenClient($token);

switch ($_POST['type']) {
	case '1':
		db();
		break;
	case '2':
		# code...
		break;
	default:
		# code...
		break;
}

function db(){

	$method = 'byai.openapi.calljob.list';//要调用的api名称

	$api_version = '1.0.0';//要调用的api版本号

	$my_params = [
	    'companyId' => '44748'
	];

	$renwulist=getBYapi($method,$my_params)['data']['list'];

	$arr_sum=[];

	for($i=0;$i<count($renwulist);$i++){


		$callJobId=$renwulist[$i]['callJobId'];


		$method1 = 'byai.openapi.calljob.calldone.list'; //要调用的api名称

		$my_params1 = [
		    'callJobId' => $callJobId,
		    'companyId' => '44748',
		    'finishStatus'=>'0',
		    'pageSize'=>500,

		];

		$pages=getBYapi($method1, $my_params1)['data']['pages'];

		//echo "总页数：".$pages."<br>";

		for($j=1;$j<=$pages;$j++){

			$my_params2 = [

			    'callJobId' => $callJobId,
			    'companyId' => '44748',
			    'finishStatus'=>'0',
			    'pageSize'=>500,
			    'pageNum'=>$j

			];

			$list=getBYapi($method1, $my_params2)['data']['list'];

			for($k=0;$k<count($list);$k++){


				array_push($arr_sum,$list[$k]);

			}


		}
		

	}

	echo insert_DB($arr_sum);
}




function insert_DB($data){



	$host='localhost';
	$user='root';
	$password='root';
	$database='weizandata';
	$link=mysqli_connect($host, $user, $password, $database);
	$link->query("set names utf8");

	$j;$e;

	for($i=0;$i<count($data);$i++){



	   	$qure="INSERT INTO BY_data (chatRound,callInstanceId,callJobId,finishStatusDesc,customerTelephone,callJobName,customerName,duration,finishStatus,luyinOssUrl,callInstanceStatus,startTime,endTime,resultList_name,resrltList_value) VALUE({$data[$i]['chatRound']},'{$data[$i]['callInstanceId']}','{$data[$i]['callJobId']}','{$data[$i]['finishStatusDesc']}','{$data[$i]['customerTelephone']}','{$data[$i]['callJobName']}','{$data[$i]['customerName']}',{$data[$i]['duration']},{$data[$i]['finishStatus']},'{$data[$i]['luyinOssUrl']}',{$data[$i]['callInstanceStatus']},'{$data[$i]['startTime']}','{$data[$i]['endTime']}','{$data[$i]['resultList'][0]['name']}','{$data[$i]['resultList'][0]['value']}');";

		$result=mysqli_query($link, $qure);


	   	if($result){

	   		$j++;
	   	}else if($result=='1062'){

	   		$c++;

	   	}else{

	   		$e++;
	   		//$erro.= '语句：'.$qure.'<br>错误代码: '.mysqli_errno($link).'<br> 错误解释: '.mysqli_error($link).'<br>';
	   	}


	}

	mysqli_close($link);

	$arr=['sucsse'=>$j,'erro'=>$e,'chongFu'=>$c];

	$json=json_encode($arr,JSON_UNESCAPED_UNICODE);

	return $json;


}

function getBYapi($method,$my_params,$my_files=array()){


	require_once __DIR__ . '/baiyingSDK/open-sdk-php/lib/BYTokenClient.php';
	require_once __DIR__ . '/baiyingSDK/open-sdk-php/lib/BYHttpClient.php';
	$token = 'f33160b4db6d39a3ae2091ce99796372';//请填入后获取的access_token
	$client = new BYTokenClient($token);


	$api_version = '1.0.0';//要调用的api版本号

	$renwulist=$client->post($method, $api_version,$my_params);

	return $renwulist;

}

